package com.itheima.app.manager;

import cn.hutool.core.collection.CollectionUtil;
import com.itheima.app.interceptor.UserHolder;
import com.itheima.domain.db.*;
import com.itheima.service.db.BlackListService;
import com.itheima.service.db.NotificationService;
import com.itheima.service.db.QuestionService;
import com.itheima.service.db.UserInfoService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.SettingVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SettingManager {

    @DubboReference
    private QuestionService questionService;

    @DubboReference
    private NotificationService notificationService;


    public ResponseEntity findSettingVo() {

        Long userId = UserHolder.get().getId();

        Question question = questionService.findByUserId(userId);

        Notification notification = notificationService.findByUserId(userId);

        SettingVo vo = new SettingVo();

        vo.setId(userId);

        if (question != null) {
            vo.setStrangerQuestion(question.getStrangerQuestion());
        }
        vo.setPhone(UserHolder.get().getMobile());
        if (notification != null) {
            vo.setGonggaoNotification(notification.getGonggaoNotification());
            vo.setLikeNotification(notification.getLikeNotification());
            vo.setPinglunNotification(notification.getPinglunNotification());
        }
        return ResponseEntity.ok(vo);
    }

    public void setQuestion(String content) {
        Long userId = UserHolder.get().getId();
        Question question = questionService.findByUserId(userId);
        if (question == null) {
            question = new Question();
            question.setUserId(userId);
            question.setStrangerQuestion(content);
            questionService.save(question);
        } else {
            question.setStrangerQuestion(content);
            questionService.update(question);

        }

    }

    public void setNotification(Notification param) {

        Long userId = UserHolder.get().getId();

        Notification notification = notificationService.findByUserId(userId);
        if (notification == null) {
            param.setUserId(userId);
            notificationService.save(param);
        } else {
            notification.setGonggaoNotification(param.getGonggaoNotification());
            notification.setLikeNotification(param.getLikeNotification());
            notification.setPinglunNotification(param.getPinglunNotification());
            notificationService.update(notification);
        }
    }


    @DubboReference
    private BlackListService blackListService;

    @DubboReference
    private UserInfoService userInfoService;

    public ResponseEntity findBalckListByPageBeanVo(Integer pageNum, Integer pageSize) {
        //1.获取用户id
        Long userId = UserHolder.get().getId();
        //2.获取分页对象
        PageBeanVo pageBeanVo = blackListService.findByPage(userId, pageNum, pageSize);
        //3.根据黑名单id查询userinfo

        List<BlackList> list = (List<BlackList>) pageBeanVo.getItems();

        List<UserInfo> userInfoList = new ArrayList<>();

        if (CollectionUtil.isNotEmpty(list)) {
            for (BlackList blackList : list) {
                Long blackUserId = blackList.getBlackUserId();
                UserInfo userInfo = userInfoService.findById(blackUserId);
                userInfoList.add(userInfo);
            }
        }

        pageBeanVo.setItems(userInfoList);

        return ResponseEntity.ok(pageBeanVo);
    }

    public void deleteBlackList(Long blackUserId) {
        Long userId = UserHolder.get().getId();
        blackListService.delete(userId, blackUserId);
    }
}
